/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rmatos.programming.blog.presentation;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author rmatos
 */
@ManagedBean(name = "FlowManagedBean")
@SessionScoped
public class FlowManagedBean {

    private String username;
    private String password;

    /** Creates a new instance of FlowManagedBean */
    public FlowManagedBean() {
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String forgotYourPassword() {

        return "www.google.com";

    }
    
    public String login() {

        if ((username.equals("rudy")) && (password.equals("matos"))) {

            HttpSession session = ((HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true));
            session.setAttribute("username", username);
            session.setMaxInactiveInterval(60 * 5);

        } else {

            return "error";

        }

        return "index";

    }

    public boolean checkIfTheUserIsAdmin() {

        boolean isAdmin = false;

        try {

            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);

            if (session.getAttribute("username").toString().length() > 0) {
                
                isAdmin = true;

            }

        } catch (NullPointerException e) {

            return isAdmin;

        }

        return isAdmin;


    }

    public String goToLogin() {

        return "login";

    }

    public String goToAdmin() {

        String _pageToReturn = "";

        if (((HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true)).isNew()) {

            _pageToReturn = "login";

        } else {

            _pageToReturn = "adminpage";

        }

        return _pageToReturn;

    }
}
